package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.ScoreDisplayRulesTeacherJdbc;
import com.wuseguang.springcloud.dao.mapper.ScoreDisplayRulesTeacherMapper;
import com.wuseguang.springcloud.dao.repository.ScoreDisplayRulesTeacherRepository;
import com.wuseguang.springcloud.entity.ScoreDisplayRulesTeacher;
import com.wuseguang.springcloud.service.ScoreDisplayRulesTeacherService;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 教师关联分数显示规则表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-02-02
 */
@Service
public class ScoreDisplayRulesTeacherServiceImpl implements ScoreDisplayRulesTeacherService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ScoreDisplayRulesTeacherRepository scoreDisplayRulesTeacherRepository;
    @Autowired
    private ScoreDisplayRulesTeacherMapper scoreDisplayRulesTeacherMapper;
    @Autowired
    private ScoreDisplayRulesTeacherJdbc scoreDisplayRulesTeacherJdbc;

    @Override
    public List<Map<String, Object>> findScoreDisplayRules(String teacherId) {

        //查询教师的显示规则
        String  sql = "select sdr.*,if(IFNULL(sdrt.id,0)>0,1,0) state from score_display_rules sdr left join " +
                "score_display_rules_teacher sdrt on(sdr.id=sdrt.score_display_rules_id and sdrt.teacher_id=?) ORDER BY sdr.id ";


        return scoreDisplayRulesTeacherJdbc.queryListMap(sql,teacherId);
    }

    @Override
    public Result addAndUpdateScoreDisplayRulesTeacher(Integer scoreDisplayRulesId, String teacherId) {

        //根据教师id查询教师的显示规则
        ScoreDisplayRulesTeacher sdrt =  scoreDisplayRulesTeacherRepository.findByTeacherId(Integer.valueOf(teacherId));
        //对象是否为空
        if(sdrt == null){
            //没有就新增
            sdrt = new ScoreDisplayRulesTeacher();
            sdrt.setScoreDisplayRulesId(scoreDisplayRulesId);
            sdrt.setTeacherId(Integer.valueOf(teacherId));
        }else{
            //有就修改
            sdrt.setScoreDisplayRulesId(scoreDisplayRulesId);
        }

        scoreDisplayRulesTeacherRepository.save(sdrt);
        return new Result();
    }
}
